package com.latitech.flutter_push

import android.content.Context
import android.content.Intent
import android.os.Bundle
import com.alibaba.sdk.android.push.MessageReceiver
import com.alibaba.sdk.android.push.notification.CPushMessage
import org.json.JSONObject

/**
 * 阿里云消息接收器
 *
 * @author 超悟空
 * @version 1.0 2019/11/4
 * @since 1.0 2019/11/4
 **/
class FlutterPushMessageReceiver : MessageReceiver() {

    override fun onNotificationOpened(context: Context, title: String?, summary: String?, extraMap: String?) {
        val intent = Intent(PLUGIN_OPEN_NOTIFICATION_ACTION)

        intent.putExtra(PLUGIN_PUSH_EXTRAS, Bundle().apply {
            putString("title", title)
            putString("summary", summary)
            putString("extraMap", extraMap)
        })

        context.sendBroadcast(intent)
    }

    override fun onNotification(context: Context, title: String?, summary: String?, extraMap: MutableMap<String, String>?) {
        val intent = Intent(PLUGIN_RECEIVE_NOTIFICATION_ACTION)

        intent.putExtra(PLUGIN_PUSH_EXTRAS, Bundle().apply {
            putString("title", title)
            putString("summary", summary)
            if (extraMap != null) {
                putString("extraMap", JSONObject(extraMap as Map<*, *>).toString())
            }
        })

        context.sendBroadcast(intent)
    }

    override fun onMessage(context: Context, message: CPushMessage) {
        val intent = Intent(PLUGIN_RECEIVE_MESSAGE_ACTION)

        intent.putExtra(PLUGIN_PUSH_EXTRAS, Bundle().apply {
            putString("id", message.messageId)
            putString("title", message.title)
            putString("content", message.content)
        })

        context.sendBroadcast(intent)
    }

    override fun onNotificationRemoved(p0: Context?, p1: String?) {
    }

    override fun onNotificationClickedWithNoAction(p0: Context?, p1: String?, p2: String?, p3: String?) {
    }

    override fun onNotificationReceivedInApp(p0: Context?, p1: String?, p2: String?, p3: MutableMap<String, String>?, p4: Int, p5: String?, p6: String?) {
    }
}